import settings from "@/settings";

/**
 * HTML 附加内容渲染器
 * @author `wangxin 13700043840@163.com` 2020/4/17
 */
export class HTMLRenderer {
  /** 装载 HTML 渲染器 */
  install() {
    const { title, version, author, company, description } = settings.parse("");
    const metas = {
      description,
      keywords: [title].join(','),
      company,
      author,
      version
    };
    Object.keys(metas).map(p => {
      if (!metas[p]) { return; }
      let element = document.createElement('meta');
      element.setAttribute('name', p);
      element.setAttribute('content', metas[p]);
      document.head.append(element);
    });
  }
}

/**
 * 资源内容渲染器
 * 例如 script/link等。
 * @private
 * @description 此处主要处理附加字体图标
 * @example
 * // setting.js
 * {
 *   icons: {
 *       'management': '/font_1761388_5vveoip5b8/iconfont.js',
 *       'svgs': '/font_1766603_k0ir7lmnv1/iconfont.js',
 *       'smp-icon icons': '//at.alicdn.com/t/font_1266287_dsomf9kkemc.css'
 *   }
 * }
 */
export class ResourceRenderer {
  /** 装载 资源 渲染器 */
  install() {
    const icons = settings.parse("icons");
    Object.keys(icons).map(p => {
      const v = icons[p];
      let element;
      if (v.endsWith('.js')) {
        element = document.createElement('script');
        element.setAttribute('type', 'text/javascript');
        element.setAttribute('src', v);
      } else {
        element = document.createElement('link');
        element.setAttribute('rel', 'stylesheet');
        element.setAttribute('href', v);
      }
      document.head.append(element);
    });
  }
}
